<template>
  <div class="page-container">
    <div class="common-content">
      <!-- 列表搜索条件部分 label宽度要固定,优先展示输入框和下拉框 ;时间选择和地区选择放置在最后,因为后两个的尺寸表较大,换行后会导致整体对不齐 -->
      <div class="search-wrap">
        <el-form
          :inline="true"
          :model="searchFilters"
          :size="searchSize"
          ref="searchFilters"
          label-position="right"
          label-width="100px"
        >
          <el-row class="search-input">
            <el-form-item label="证书名称：" prop="certificateName">
              <el-input v-model="searchFilters.certificateName" placeholder="请输入证书名称"></el-input>
            </el-form-item>
            <el-form-item label="证书编号：" prop="certificateNumber">
              <el-input v-model="searchFilters.certificateNumber" placeholder="请输入证书编号"></el-input>
            </el-form-item>
            <el-form-item label="签发机构：" prop="issuingAuthority">
              <el-input v-model="searchFilters.issuingAuthority" placeholder="请输入签发机构"></el-input>
            </el-form-item>
          </el-row>
          <el-row class="search-input">
            <el-form-item label="商品编码：" prop="productCode">
              <el-input v-model="searchFilters.productCode" placeholder="请输入商品编码"></el-input>
            </el-form-item>
            <el-form-item label="商品名称：" prop="productName">
              <el-input v-model="searchFilters.productName" placeholder="请输入商品名称"></el-input>
            </el-form-item>
            <el-form-item label="状态：" prop="status">
              <el-select v-model="searchFilters.status" placeholder="请选择状态">
                <el-option label="启用" value="0"></el-option>
                <el-option label="停用" value="1"></el-option>
              </el-select>
            </el-form-item>
          </el-row>
          <!-- 搜索区域按钮-查询,重置 -->
          <div class="tool-wrap">
            <el-row class="search-button">
              <el-button-group>
                <el-button type="primary" size="small" @click="findPage()">查询</el-button>
                <el-button type="primary" size="small" @click="resetForm('searchFilters')">重置</el-button>
              </el-button-group>
            </el-row>
          </div>
          <!-- 表格数据部分 -->
          <div class="table-wrap">
            <el-table
              :data="productQualifications"
              :header-cell-style="{ 'color': '#FFF','background-color': '#2F4554'}"
              style="width:100%"
              v-loading="productQualificationsLoading"
            >
              <el-table-column prop="id" label="ID" align="center"></el-table-column>
              <el-table-column prop="productCode" label="商品编码" align="center"></el-table-column>
              <el-table-column prop="productName" label="商品名称" align="center"></el-table-column>
              <el-table-column prop="certificateNumber" label="证书编码" align="center"></el-table-column>
              <el-table-column prop="certificateName" label="证书名称" align="center"></el-table-column>
              <el-table-column prop="issuingAuthority" label="签发机构" align="center"></el-table-column>
              <el-table-column prop="status" label="状态" align="center">
                <template slot-scope="scope">
                  <span v-if="scope.row.status == 0">启用</span>
                  <span v-if="scope.row.status == 1">停用</span>
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="primary"
                    @click="handleDetail(scope)"
                  >查看</el-button>
                  <el-button
                    size="mini"
                    type="primary"
                    v-if="scope.row.status == 1 && deptType == 1"
                    @click="handleEdit(scope)"
                  >编辑</el-button>
                  <el-button
                    size="mini"
                    type="primary"
                    v-if="scope.row.status == 0  && (deptType == 0 || deptType == 2)"
                    @click="handelStatus(scope,1)"
                  >停用</el-button>
                  <el-button
                    size="mini"
                    type="primary"
                    v-if="scope.row.status == 1 && (deptType == 0 || deptType == 2)"
                    @click="handelStatus(scope,0)"
                  >启用</el-button>
                </template>
              </el-table-column>
            </el-table>
            <div class="toolbar" style="padding:10px;">
              <el-pagination
                layout="total, prev, pager, next, jumper,sizes"
                @current-change="handlePageChange"
                :current-page="searchFilters.pageNum"
                :page-size="searchFilters.pageSize"
                :total="totalSize"
                style="float:right;"
                :page-sizes="[10, 20, 30, 40]"
                @size-change="handleSizeChange"
              ></el-pagination>
            </div>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
//二.数据输出区
export default {
  //2.2 数据初始化区
  data() {
    return {
      //2.2.1 公共数据初始化
      totalSize: 0, //// 返回数据总条数
      productQualifications: [], // 表格列表数据
      productQualificationsLoading: false,
      searchSize: "mini",
      searchFilters: {
        certificateName: null,
        certificateNumber: null,
        productCode: null,
        productName: null,
        status: null,
        pageNum: 1,
        pageSize: 10,
      },
      deptType:localStorage.getItem('deptType'),
    };
  },
  methods: {
    findPage() {
      // 传过滤器的参数
      let this_ = this;
      this_.productQualificationsLoading = true;
      this.utils.request.queryProductQualifications(
        this_.searchFilters,
        function (res) {
          if (res.code === "0000" && res.data && res.data.rows) {
            this_.productQualifications = res.data.rows;
            this_.totalSize = Number(res.data.total);
          } else {
            this_.productQualifications = [];
            this_.totalSize = 0;
          }
          this_.productQualificationsLoading = false;
        }
      );
    },

    // 3.重置搜索框条件
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.findPage();
    },

    // 4.详情按钮对应的方法(TODO 需要修改路径)
    handleDetail(params) {
      this.$router.push({
        path: "/getQualificationsManageDetail/" + params.row.productCode + "/" + 1,
      });
    },

    //停用或启用
    handelStatus(params,status){
      let param = {
        productCode : params.row.productCode,
        status:status
      };
      this.utils.request.saveOrUpdateProductQualificationsInfo(param,res =>{
        if(res.code == '0000'){
          this.findPage();
          this.$message({message:"操作成功！",type:"success"});
        }else{
          this.$message({message:"操作失败！",type:"error"});
        }
      });
    },
    // 4.详情按钮对应的方法(TODO 需要修改路径)
    handleEdit(params) {
      this.$router.push({
        path: "/getQualificationsManageDetail/" + params.row.productCode + "/" + 0,
      });
    },
    // 5.请求条数发生变化
    handleSizeChange(pageSize) {
      this.searchFilters.pageSize = pageSize;
      this.findPage();
    },

    // 6.请求页数发生变化
    handlePageChange(pageNum) {
      this.searchFilters.pageNum = pageNum;
      this.findPage();
    },
  },
  //1.初始化页面调用方法(dom元素绑定后再执行初始化数据操作)
  mounted() {
    this.findPage();
  },
};
</script>
<style lang="scss" scoped>
/* ele组件样式重置 */
.common-content {
  padding: 10px;
  box-sizing: border-box;

  .search-wrap /deep/ {
    .el-form-item__label {
      height: 32px;
      line-height: 32px;
    }

    .el-form-item__content {
      height: 32px;
      line-height: 32px;
      width: 150px;
    }

    .el-input--mini .el-input__inner {
      height: 32px;
      line-height: 32px;
    }

    .el-date-editor.el-input,
    .el-date-editor.el-input__inner {
      width: 150px;
    }
  }

  /* 多日期样式重置 */
  .el-dater /deep/ .el-form-item__content {
    width: 330px;
  }

  /* 通用工具部分样式 */
  .tool-wrap {
    width: 100%;
    height: auto;
    position: relative;
    overflow: hidden;

    .left-area {
      float: left;
      width: 50%;
      text-align: left;

      button {
        margin-right: 15px;
        border-radius: 5px;
      }
    }

    .right-area {
      float: right;
      width: 50%;
      text-align: right;
    }
  }

  /* 表格部分样式 */
  .table-wrap {
    width: 100%;
    max-height: 600px;
    margin-top: 20px;
  }

  /* 弹窗样式 */
  .dialog-wrap /deep/ .el-dialog__footer {
    text-align: center !important;
  }
}
</style>
